package com.xu.sys.servlet;

import com.xu.sys.bean.SysMenu;
import com.xu.sys.bean.SysRole;
import com.xu.sys.bean.SysRoleMenu;
import com.xu.sys.bean.SysUser;
import com.xu.sys.service.IMenuService;
import com.xu.sys.service.IRoleService;
import com.xu.sys.service.impl.MenuServiceimpl;
import com.xu.sys.service.impl.RoleServiceimpl;
import com.xu.sys.utils.Constant;
import com.xu.sys.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(name = "roleServlet",urlPatterns = {"/sys/roleServlet"})
public class RoleServlet extends BaseServlet{
    private IRoleService service = new RoleServiceimpl();
    private IMenuService menuService = new MenuServiceimpl();
    @Override
    public void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        super.list(req, resp);//封装了分页的相关操作
        //写自己的查询
        service.listPage(pageUtils);
        req.setAttribute(Constant.LIST_PAGE_UTILS,pageUtils);
        //页面跳转
        req.getRequestDispatcher("/sys/role/list.jsp").forward(req,resp);;
    }

    @Override
    public void saveOrUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter("id");
        //查询出所有的菜单信息
        List<SysMenu> menus = menuService.list(null);
        req.setAttribute("menus",menus);
        if(StringUtils.isNotEmpty(id)){
            int roleId = Integer.parseInt(id);
            //根据当前的角色编号查询出已经分配的菜单信息
            List<SysRoleMenu> roleMenus = service.queryByRoleId(roleId);
            if (roleMenus != null && roleMenus.size() > 0){
                List<Integer> owenrMenus =  roleMenus.stream().map(item->item.getMenuId()).collect(Collectors.toList());
                for (SysMenu menu : menus){
                    if (owenrMenus.contains(menu.getId())){
                        menu.setCheck(true);
                    }
                }
            }

            //更新数据
            SysRole entity =  service.findById(Integer.parseInt(id));
            req.setAttribute("entity",entity);
        }


        req.getRequestDispatcher("/sys/role/addOrUpdate.jsp").forward(req,resp);
    }

    @Override
    public void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        // 获取提交的数据
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String notes = req.getParameter("notes");
        SysRole entity = new SysRole();
        entity.setNotes(notes);
        entity.setName(name);
        //获取分配的表单
        String[] menuIds = req.getParameterValues("menuId");
        int roleId = Integer.parseInt(id);
        if(StringUtils.isNotEmpty(id)){
            // 更新
            entity.setId(Integer.parseInt(id));
            service.updteById(entity);
            //更新分配的菜单
            //删除当前角色分配的所有菜单
            service.deleteMenuByRoleId(Integer.parseInt(id));
            //插入新分配的菜单
            if(menuIds != null && menuIds.length > 0){
                for(String menuId : menuIds){
                    service.saveDispatcherMenu(roleId,menuId);
                }
            }

        }else {
            // 保存数据
            service.save(entity);
        }
        // 做重定向查询
        resp.sendRedirect("/sys/roleServlet?action=list");

    }

    @Override
    public void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //删除角色
        String id = req.getParameter("id");
        // 删除角色信息 我们需要做校验 如果该角色已经分配给了用户。那么这个角色就不能被删除
        boolean flag = service.checkRoleDispatch(Integer.parseInt(id));
        PrintWriter writer = resp.getWriter();
        if(flag){
            //表示没有分配，可以删除
            service.deleteById(Integer.parseInt(id));
            writer.write("ok");
        }else{
            //表示不能被删除
            writer.write("error");
        }
        writer.flush();
        writer.close();

    }

    @Override
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    }


}
